<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/29 23:02
 */

namespace app\admin\business;


use app\common\business\BusBase;
use app\common\model\mysql\AdminGroup as AdminGroupModel;
use think\facade\Log;

class AdminGroup extends BusBase {

    public $model = null;

    public function __construct() {
        $this->model = new AdminGroupModel();
    }

    /**
     * 新增逻辑
     * @param $id
     * @param $roleIds
     * @return bool
     */
    public function addAll($id, $roleIds){
        if (!$id) {
            return false;
        }
        if (empty($roleIds)) {
            return false;
        }

        $data = [];
        $group = explode(',', $roleIds);
        foreach ($group as $k => $g) {
            $data[] = [
                'roleid' => $g,
                'adminid' => $id,
            ];
        }
        $this->model->startTrans();
        try {
            $this->model->setDelete($id);
            $this->model->saveAll($data);
            // 提交事务
            $this->model->commit();
        } catch (\Exception $e) {
            Log::error('common-business-AdminGroup-add : ' . $e->getMessage());
            // 回滚事务
            $this->model->rollback();
            return false;
        }
        return true;
    }

}